package main_com.Controlers.login;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import main_com.mapper.UserMapper;
import main_com.pojo.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/RegisterController")
public class RegisterController {
    @Autowired
    UserMapper userMapper;

    // 新增的方法，用于验证用户名是否存在并返回结果给前端
    @RequestMapping("/checkUsername")
    @ResponseBody
    protected Map<String, Boolean> checkUsername(HttpServletRequest request) {
        Map<String, Boolean> resultMap = new HashMap<>();
        String userName = request.getParameter("username");
        try {
            int count = userMapper.findUserName(userName);
            resultMap.put("exists", count > 0);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("exists", false);  // 出现异常时，默认认为用户名不存在（可根据实际需求调整错误处理逻辑）
        }
        return resultMap;
    }

    @RequestMapping("/register")
    protected void register(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String userName = request.getParameter("username");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birth = request.getParameter("year") + "-" + request.getParameter("mouth") + "-" + request.getParameter("day");
        String nation = request.getParameter("nation");
        String edu = request.getParameter("edu");
        String work = request.getParameter("work");
        String phone = request.getParameter("phone");
        String place = request.getParameter("place");
        String email = request.getParameter("email");
        if (userName.isEmpty() || password1.isEmpty() || password2.isEmpty() || name.isEmpty() || phone.isEmpty() || email.isEmpty()) {
            response.sendRedirect("http://localhost/register/register.jsp");
        } else if (!(password1.equals(password2))) {
            response.sendRedirect("http://localhost/register/register.jsp");
        } else {
            try {
                int k;
                k = userMapper.findUserName(userName);
                if (k > 0) {
                    response.sendRedirect("http://localhost/register/register.jsp");
                    return;
                } else {
                    userMapper.insertUser(new UserBean(userName, password1, name, sex, birth, nation, edu, work, phone, place, email));
                }
                response.sendRedirect("http://localhost/login.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}